<template>
  <div id="recruitlist">
    <!-- 面包屑 -->
    <el-breadcrumb separator=">">
      <el-breadcrumb-item :to="{ path: '/' }">系统首页</el-breadcrumb-item>
      <el-breadcrumb-item>优秀年轻干部</el-breadcrumb-item>
    </el-breadcrumb>
    <section class="wboxs">
    <!--工具条-->
      <div>
        <el-date-picker
          v-if="inRoles('ROLE_admin') || inRoles('ROLE_youngadmin')"
          v-model="theYear"
          type="year"
          placeholder="选择年度"
          value-format="yyyy">
        </el-date-picker>
        <el-button v-if="inRoles('ROLE_admin') || inRoles('ROLE_youngadmin')" icon="el-icon-plus" type="success" @click="editFormSubmit" :loading="btnLoading" style="margin-right:20px">新增年度并设为当前年度</el-button>
        <span style="line-height: 40px;" v-if="yearList&&yearList.find(item=>item.isCurrent)">当前年度：{{yearList.find(item=>item.isCurrent).theYear}}年</span>
        <!-- <span v-else>未设置当前招聘工作年度，填报单位用户界面公开招聘模块暂时关闭</span> -->
      </div>
    </section>
    <section class="wboxd" style="margin-top:10px">
      <!--列表-->
      <el-table :data="yearList" highlight-current-row v-loading="listLoading" style="width: 100%;border-top:1px solid #9CA3B3" :height="tableHeight" border id="selectionindexlist" row-key="personId"
        default-expand-all
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
        <el-table-column label="序号" type="index" align="center" width="100" />
        <el-table-column label="年度" align="center" prop="theYear" />
        <el-table-column prop="isCurrent" label="是否当前年度" width="160" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.isCurrent===1" type="success" effect="dark" style="cursor: default;">是</el-tag>
            <template v-else>
              <el-popconfirm
                v-if="inRoles('ROLE_admin') || inRoles('ROLE_youngadmin')"
                :title='`确定要设置当前年度为${scope.row.theYear}吗？`'
                @confirm="handleSetCurrent(scope.row.theYear,1)"
              >
                <el-tag slot="reference" type="info" effect="dark" style="cursor: pointer;">否</el-tag>
              </el-popconfirm>
              <el-tag v-else type="info" effect="dark" style="cursor: default;">否</el-tag>
            </template>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="260">
          <template slot-scope="scope">
            <template>
              <el-button size="small"  @click="handleInto(scope.row.theYear)" :loading="btnLoading" style="margin:10px 0">进入</el-button>
              <el-button v-if="inRoles('ROLE_admin') || inRoles('ROLE_youngadmin')" type="danger" size="small" @click="handleDel(scope.row.id)" :loading="btnLoading" style="margin:10px 0">删除</el-button>
            </template>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div>
        <el-pagination style="float:right" :total="totalnum" :page-size="pageSize" @current-change="handleCurrentChange" :current-page.sync="pageNum" @size-change="handleSizeChange" :page-sizes="[10, 20, 30, 50]" layout="total, sizes, prev, pager, next, jumper"></el-pagination>
      </div>
    </section>
  </div>
</template>

<script>
export default {
  data () {
    return {
      saveloading: false,
      tableHeight: 100,
      btnLoading: false,
      theYear: null,
      listLoading: false,
      yearList: [],
      pageNum: 1,
      pageSize: 20,
      totalnum: null,
      changeSizeFlag: false
    }
  },
  methods: {
    // 新增年度
    editFormSubmit () {
      if (!this.theYear) {
        this.$message({
          message: '请先选择年度',
          type: 'error'
        })
        return false
      }
      if (this.yearList.find(item => item.theYear === parseInt(this.theYear))) {
        this.$message({
          message: '该年度已存在',
          type: 'error'
        })
        return false
      }
      this.btnLoading = true
      let para = {
        theYear: this.theYear,
        isCurrent: 1
      }
      this.$http.post(this.$http.defaults.youngAPI + `api/busAnnual/save`, para).then(response => {
        console.log(response.data.success)
        if (response.data.success === true) {
          this.$message({
            message: '设置成功',
            type: 'success'
          })
          this.theYear = null
          this.btnLoading = false
          this.getList()
        } else {
          console.log('设置失败:' + response.data.message)
          this.$message({
            message: '设置失败:' + response.data.message,
            type: 'error'
          })
          this.btnLoading = false
        }
      }).catch(error => {
        console.log(error)
        this.$message({
          message: '设置失败:' + error,
          type: 'error'
        })
        this.btnLoading = false
      })
    },
    // 获取列表
    getList () {
      let para = {
        pageNum: this.pageNum,
        pageSize: this.pageSize
      }
      this.listLoading = true
      this.$http
        .post(this.$http.defaults.youngAPI + `api/busAnnual/queryByPage`, para)
        .then(response => {
          if (response.data.success) {
            this.listLoading = false
            this.yearList = response.data.result.records
            this.totalnum = response.data.result.total
            this.changeSizeFlag = false
            if (response.data.result.current > response.data.result.pages && response.data.result.pages !== 0) {
              this.pageNum = response.data.result.pages
              this.getList()
            }
          } else {
            this.$message({
              message: '获取失败:' + response.data.message,
              type: 'error'
            })
          }
        })
        .catch(error => {
          this.$message({
            message: '网络获取失败:' + error,
            type: 'error'
          })
        })
    },
    // 设置当前年度
    handleSetCurrent (theYear, isCurrent) {
      let para = {
        theYear: theYear,
        isCurrent: isCurrent
      }
      this.listLoading = true
      this.$http.post(this.$http.defaults.youngAPI + `api/busAnnual/save`, para).then(response => {
        console.log(response.data.success)
        if (response.data.success === true) {
          this.$message({
            message: '设置成功',
            type: 'success'
          })
          this.getList()
        } else {
          console.log('设置失败:' + response.data.message)
          this.$message({
            message: '设置失败:' + response.data.message,
            type: 'error'
          })
        }
      }).catch(error => {
        console.log(error)
        this.$message({
          message: '设置失败:' + error,
          type: 'error'
        })
      })
    },
    // 删除
    handleDel (id) {
      this.$confirm('确认删除该年度吗？', '提示', {
        type: 'warning'
      }).then(() => {
        let para = {ids: [id]}
        this.btnLoading = true
        this.$http.post(this.$http.defaults.youngAPI + 'api/busAnnual/remove', para).then(response => {
          if (response.data.success === true) {
            this.btnLoading = false
            this.$message({
              message: '删除成功',
              type: 'success'
            })
          } else {
            this.btnLoading = false
            console.log('删除失败:' + response.data.message)
            this.$message({
              message: '删除失败:' + response.data.message,
              type: 'error'
            })
          }
          this.getList()
        })
          .catch(error => {
            this.btnLoading = false
            console.log(error)
            this.$message({
              message: '删除失败:' + error,
              type: 'error'
            })
          })
      })
    },
    // 进入
    handleInto (theYear) {
      let thePath = '/youngCadres/deptlist'
      if (this.inRoles('ROLE_admin') || this.inRoles('ROLE_sizhang') || this.inRoles('ROLE_fusizhang') || this.inRoles('ROLE_youngadmin')) {
        thePath = '/youngCadres/personlist'
      }
      this.$router.push({
        path: thePath,
        query: {
          theYear
        }
      })
    },
    // 判断角色
    inRoles (str) {
      let arr = JSON.parse(localStorage.getItem('Roles'))
      if (arr.indexOf(str) === -1) {
        return false
      } else {
        return true
      }
    },
    handleSizeChange (val) {
      this.changeSizeFlag = true
      this.pageNum = 1
      this.pageSize = val
      this.getList()
    },
    handleCurrentChange (val) {
      if (this.changeSizeFlag === false) {
        this.pageNum = val
        this.getList()
      }
    },
    tableresize (obj) {
      var clientHeight = 0
      if (document.body.clientHeight && document.documentElement.clientHeight) {
        clientHeight = (document.body.clientHeight < document.documentElement.clientHeight) ? document.body.clientHeight : document.documentElement.clientHeight
      } else {
        clientHeight = (document.body.clientHeight > document.documentElement.clientHeight) ? document.body.clientHeight : document.documentElement.clientHeight
      }
      obj.$nextTick(function () {
        obj.tableHeight = clientHeight - 262
      })
    }
  },
  mounted () {
    this.getList()
    this.tableresize(this)
    let _this = this
    window.onresize = function windowResize () {
      // 通过捕获系统的onresize事件触发我们需要执行的事件
      _this.tableresize(_this)
    }
  }
}
</script>
<style>
.selectioncontent{
  border-bottom:1px solid #333;
  padding:5px !important;
}
  .pointtab .el-badge__content.is-fixed{
    right: -5px;
    top:18px;
  }
  .pointtab{
    padding-right:22px;
  }
  .pointRadio .el-badge__content.is-fixed{
    right: -5px;
    top:6px;
  }
  .pointRadio .el-radio-button__inner{
    height:40px;
    padding-left:15px;
    padding-right:42px;
  }
#recruitlist .el-table--border td, .el-table--border th, .el-table__body-wrapper .el-table--border.is-scrolling-left~.el-table__fixed{
  border-bottom:1px solid #95B8E7;
}
#recruitlist .el-table td, #recruitlist .el-table th.is-leaf{
  border-right:1px solid #95B8E7;
}
  .wboxd .el-table th{
    font-weight: normal;
    background-image: linear-gradient(to bottom , #f8f8f8, #efefef);
    text-align: center;
    border-right: #95B8E7 solid 1px;
    border-bottom: #95B8E7 solid 1px;
    min-height: 30px;
    line-height:30px;
  }
  .wboxd .el-table td{
    padding:0;
    font-size:var(--size14) !important;
    line-height: 20px;
    font-family: '宋体',
  }
  .tableth{
    background-image: linear-gradient(to bottom , #f8f8f8, #efefef);
    height:33px;
  }
  #recruitlist .wboxs{
    height:45px;
    padding-top:10px;
  }
  #recruitlist .el-table__row{
    height: 50px;
  }
  #recruitlist #legent{
    float:right;
    margin-right: 20px;
  }
  #recruitlist #legent div{
    float:left;
    line-height: 42px;
    font-size:var(--size12);
  }
  #recruitlist #legent i{
    width: 30px;
    height: 30px;
    background-position: center center;
    background-repeat: no-repeat;
    float: left;
    display: block;
    margin:5px 0px 0 3px;
  }
  #recruitlist #selectionindexlist .cell i.custom{
    width: 30px;
    height: 30px;
    background-position: center center;
    background-repeat: no-repeat;
    float: center;
    display: block;
    margin:0 auto;
  }
  #recruitlist .el-step__title.is-finish {
    color: #409EFF;
  }
  #recruitlist .el-step__title {
    font-size:12px;
    line-height: 22px;
    padding-left: 8px;
  }
  #recruitlist .el-steps{
    margin:10px 0;
  }
  .el-tooltip__popper.is-dark{
    font-size:var(--size18) !important;
  }
  .el-tabs--card>.el-tabs__header {
    border-bottom: 1px solid #9CA3B3;
  }
  .el-tabs--card>.el-tabs__header .el-tabs__nav {
    border: 1px solid #9CA3B3;
    border-bottom: 0;
  }
  .el-tabs--card>.el-tabs__header .el-tabs__item {
    border-left: 1px solid #9CA3B3;
    /* background-color: #F0F0F0; */
  }
  .el-tabs__item.is-active {
    color: #0000FF !important;
    font-weight: bolder !important;
    /* background-color: #fff !important; */
    /* border-bottom: 2px solid #9CA3B3 !important; */
  }
</style>
<style scoped>
.mytable{
  border-top: 1px solid #74a6cc;
  border-left: 1px solid #74a6cc;
  margin:0 10px 10px;
  width: calc(100% - 20px);
}
.mytable th{
  padding: 10px;
  border-right: 1px solid #74a6cc;
  border-bottom: 1px solid #74a6cc;
  vertical-align: top;
  line-height: 150%;
  background: #FAFAFA;
  width:250px;
  text-align:right;
}
.mytable td{
  padding: 10px;
  border-right: 1px solid #74a6cc;
  border-bottom: 1px solid #74a6cc;
  vertical-align: top;
  line-height: 150%;
}
  .el-tooltip__popper{
    font-size:var(--size) !important;
  }
  h1{
    font-size:var(--size16) !important;
    font-weight: bolder;
    height: 30px;
    border-bottom: 1px solid #95B8E7;
    font-family: '宋体';
    line-height: 30px;
    color: #000;
    padding-left:10px;
    background-image: linear-gradient(to bottom , #EEF4FF, #E0ECFF);
    margin:0;
  }
  .el-pagination{
    float: left;
    padding:7px 10px;
  }
  .wboxs{
    padding:5px 15px;
    width: calc(100% - 30px);
    background: #fff;
    border: 1px solid #95B8E7;
    margin:0 10px;
    overflow: hidden;
  }
  .wboxd{
    padding:0;
    width: calc(100% - 0px);
    background: #fff;
    border: 1px solid #95B8E7;
    margin:0 10px;
    overflow: hidden;
  }
</style>
